package com.taijia.chapter6;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import com.alibaba.fastjson.JSON;

public class TestFile {
	
	public static void testFile() {
		String fileName = "txt.txt";
		String pathName = "path1" + File.separator + "path2";
		File file = new File(pathName, fileName);
		if (file.exists()) {
			System.out.println("文件路径:" + file.getAbsolutePath());
			System.out.println("文件大小:" + file.length());
			System.out.println("文件可读:" + file.canRead());
			System.out.println("文件可写:" + file.canWrite());
			System.out.println("上层目录:" + file.getParent());
			System.out.println("最后修改时间:" + new Date(file.lastModified()));
			System.out.println("List Roots:" + JSON.toJSONString(File.listRoots()));
			file.delete();
		} else {
			file.getParentFile().mkdirs();
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void listChildren(File file,int level) {
		String levelStr = "";
		if(level > 0) {
			for(int i=0;i<level;i++) {
				levelStr+="--";
			}
		}
		level++;
		if(file.isDirectory()) {
			File[] children = file.listFiles();
			for(File child : children) {
				listChildren(child,level);
			}
		}
		System.out.println(levelStr + file.getName());
	}
	
	public static void main(String[] args) throws IOException {
		testFile();
		listChildren(new File("E:/myeclipse2014/Workspaces/2014/msb_basic"),0);
	}
	
}
